﻿Public Class frmChiTietThoiKhoaBieu

    Private Sub frmChiTietThoiKhoaBieu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LayDSCTTKB()
    End Sub

    Private Sub LayDSCTTKB()
        Try
            Dim cttkbBUS As New ChiTietThoiKhoaBieuBUS
            Dim ds As New List(Of ChiTietThoiKhoaBieuDTO)
            Dim i As Integer = 1

            ds = cttkbBUS.LayDanhSachChiTietThoiKhoaBieu()
            dgChiTietTKB.Rows.Clear()

            For Each cttkb As ChiTietThoiKhoaBieuDTO In ds
                dgChiTietTKB.Rows.Add(i, cttkb.MaChiTietTKB, cttkb.Tiet, cttkb.MaGV, cttkb.MaTKB, cttkb.MaMonHoc)
                i += 1
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThem.Click
        Try
            If (txtMaCTTKB.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Chi Tiet TKB.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (cbTiet.SelectedItem = Nothing) Then
                MessageBox.Show("Chon Tiet.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtMaGV.Text.Trim().Length = 0) Then
                MessageBox.Show("Chon Ma Giao Vien.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtMaTKB.Text.Trim.Length = 0) Then
                MessageBox.Show("Chon Ma Thoi Khoa Bieu.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtMaMH.Text.Trim.Length = 0) Then
                MessageBox.Show("Chon Ma Mon Hoc.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim cttkbDTO As New ChiTietThoiKhoaBieuDTO()
            Dim cttkbBUS As New ChiTietThoiKhoaBieuBUS()
            Dim kq As Integer = -1



            cttkbDTO.MaChiTietTKB = txtMaCTTKB.Text.Trim()
            cttkbDTO.Tiet = cbTiet.SelectedItem.ToString().Trim()
            cttkbDTO.MaGV = txtMaGV.Text.Trim()
            cttkbDTO.MaTKB = txtMaTKB.Text.Trim()
            cttkbDTO.MaMonHoc = txtMaMH.Text.Trim()

            'Kiem tra so tiet toi da
            'Lay tham so
            Dim tsBUS As New ThamSoBUS
            Dim ds As New List(Of ThamSoDTO)
            Dim SoTietToiDa As Integer = -1
            ds = tsBUS.LayDanhSachThamSo()

            For Each ts As ThamSoDTO In ds
                SoTietToiDa = Int32.Parse(ts.SoTietToiDa.Trim())
            Next

            kq = cttkbBUS.LayTongSoTietCuaGV(cttkbDTO)
            If (kq > SoTietToiDa And SoTietToiDa > 0) Then
                MessageBox.Show("Giao Vien nay da vuot So Tiet Day.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If
            ' Ket thuc kiem tra So tiet toi da

            kq = cttkbBUS.ThemChiTietThoiKhoaBieu(cttkbDTO)

            If kq > 0 Then
                LayDSCTTKB()
            Else
                MessageBox.Show("Them that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            If (ex.Message.IndexOf("create duplicate values in the index, primary key, or relationship") > 0) Then
                MessageBox.Show("Ma CTTKB da ton tai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Try
    End Sub

    Private Sub btXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoa.Click
        Try
            If (txtMaCTTKB.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Chi Tiet TKB muon Xoa.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim cttkbDTO As New ChiTietThoiKhoaBieuDTO()
            Dim cttkbBUS As New ChiTietThoiKhoaBieuBUS()
            Dim kq As Integer = -1

            cttkbDTO.MaChiTietTKB = txtMaCTTKB.Text.Trim()
            kq = cttkbBUS.XoaChiTietThoiKhoaBieu(cttkbDTO)

            If kq > 0 Then
                LayDSCTTKB()
            Else
                MessageBox.Show("Xoa that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCapNhat.Click
        Try
            If (txtMaCTTKB.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Chi Tiet TKB.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (cbTiet.SelectedItem = Nothing) Then
                MessageBox.Show("Chon Tiet.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtMaGV.Text.Trim().Length = 0) Then
                MessageBox.Show("Chon Ma Giao Vien.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtMaTKB.Text.Trim.Length = 0) Then
                MessageBox.Show("Chon Ma Thoi Khoa Bieu.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtMaMH.Text.Trim.Length = 0) Then
                MessageBox.Show("Chon Ma Mon Hoc.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim cttkbDTO As New ChiTietThoiKhoaBieuDTO()
            Dim cttkbBUS As New ChiTietThoiKhoaBieuBUS()
            Dim kq As Integer = -1

            cttkbDTO.MaChiTietTKB = txtMaCTTKB.Text.Trim()
            cttkbDTO.Tiet = cbTiet.SelectedItem.ToString().Trim()
            cttkbDTO.MaGV = txtMaGV.Text.Trim()
            cttkbDTO.MaTKB = txtMaTKB.Text.Trim()
            cttkbDTO.MaMonHoc = txtMaMH.Text.Trim()

            kq = cttkbBUS.SuaChiTietThoiKhoaBieu(cttkbDTO)

            If kq > 0 Then
                LayDSCTTKB()
            Else
                MessageBox.Show("Cap Nhat that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTim.Click
        Try
            Dim cttkbDTO As New ChiTietThoiKhoaBieuDTO()
            Dim cttkbBUS As New ChiTietThoiKhoaBieuBUS()
            Dim kq As Integer = -1

            cttkbDTO.MaChiTietTKB = txtMaCTTKB.Text.Trim()
            If (cbTiet.SelectedItem <> Nothing) Then
                cttkbDTO.Tiet = cbTiet.SelectedItem.ToString().Trim()
            End If
            cttkbDTO.MaGV = txtMaGV.Text.Trim()
            cttkbDTO.MaTKB = txtMaTKB.Text.Trim()
            cttkbDTO.MaMonHoc = txtMaMH.Text.Trim()

            Dim ds As New List(Of ChiTietThoiKhoaBieuDTO)
            Dim i As Integer = 1

            ds = cttkbBUS.TraCuuChiTietThoiKhoaBieu(cttkbDTO)
            dgChiTietTKB.Rows.Clear()

            For Each cttkb As ChiTietThoiKhoaBieuDTO In ds
                dgChiTietTKB.Rows.Add(i, cttkb.MaChiTietTKB, cttkb.Tiet, cttkb.MaGV, cttkb.MaTKB, cttkb.MaMonHoc)
                i += 1
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btXoaForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoaForm.Click
        txtMaGV.Text = ""
        txtTenGV.Text = ""
        txtMaTKB.Text = ""
        cbBuoi.SelectedItem = ""
        cbThu.SelectedItem = ""
        txtMaCTTKB.Text = ""
        cbTiet.SelectedItem = ""
        txtMaMH.Text = ""
        txtTenMH.Text = ""
    End Sub

    Private Sub btThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThoat.Click
        Me.Close()
    End Sub

    Private Sub dgChiTietTKB_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgChiTietTKB.SelectionChanged
        Try
            If (dgChiTietTKB.SelectedRows.Count > 0) Then
                If (dgChiTietTKB.SelectedRows(0).Cells(1).Value = Nothing) Then
                    Return
                End If

                txtMaCTTKB.Text = dgChiTietTKB.SelectedRows(0).Cells(1).Value.ToString().Trim()
                cbTiet.SelectedItem = dgChiTietTKB.SelectedRows(0).Cells(2).Value.ToString().Trim()
                
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btTimGV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTimGV.Click
        Try
            Dim gvDTO As New GiaoVienDTO()
            Dim gvBUS As New GiaoVienBUS()

            gvDTO.TenGV = txtTenGV.Text.Trim()

            Dim ds As New List(Of GiaoVienDTO)
            Dim i As Integer = 1
            Dim ns As String = ""

            ds = gvBUS.TraCuuGiaoVien(gvDTO)
            dgGiaoVien.Rows.Clear()

            For Each gv As GiaoVienDTO In ds
                If (gv.NgaySinh <> Date.Parse("1/1/1900")) Then
                    ns = gv.NgaySinh
                Else
                    ns = ""
                End If
                dgGiaoVien.Rows.Add(i, gv.MaGV, gv.TenGV, gv.GioiTinh, ns, gv.SoDienThoai, gv.Email, gv.DiaChi)
                i += 1
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgGiaoVien_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgGiaoVien.SelectionChanged
        Try
            If (dgGiaoVien.SelectedRows.Count > 0) Then
                If (dgGiaoVien.SelectedRows(0).Cells(1).Value = Nothing) Then
                    Return
                End If

                txtMaGV.Text = dgGiaoVien.SelectedRows(0).Cells(1).Value.ToString().Trim()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btTimTKB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTimTKB.Click
        Try
            Dim tkbDTO As New ThoiKhoaBieuDTO()
            Dim tkbBUS As New ThoiKhoaBieuBUS()

            tkbDTO.MaTKB = txtMaTKB.Text.Trim()
            If (cbThu.SelectedItem <> Nothing) Then
                tkbDTO.Thu = cbThu.SelectedItem.ToString().Trim()
            End If
            If (cbBuoi.SelectedItem <> Nothing) Then
                tkbDTO.Buoi = cbBuoi.SelectedItem.ToString().Trim()
            End If

            Dim ds As New List(Of ThoiKhoaBieuDTO)
            Dim i As Integer = 1

            ds = tkbBUS.TraCuuTKB(tkbDTO)
            dgTKB.Rows.Clear()

            For Each tkb As ThoiKhoaBieuDTO In ds
                dgTKB.Rows.Add(i, tkb.MaTKB, tkb.Thu, tkb.Buoi, tkb.MaLop, tkb.TenLop)
                i += 1
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgTKB_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgTKB.SelectionChanged
        Try
            If (dgTKB.SelectedRows.Count > 0) Then
                If (dgTKB.SelectedRows(0).Cells(1).Value = Nothing) Then
                    Return
                End If

                txtMaTKB.Text = dgTKB.SelectedRows(0).Cells(1).Value.ToString().Trim()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btTimMonHoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTimMonHoc.Click
        Try
            Dim mhDTO As New MonHocDTO()
            Dim mhBUS As New MonHocBUS()

            mhDTO.MaMonHoc = txtMaMH.Text.Trim()
            mhDTO.TenMonHoc = txtTenMH.Text.Trim()

            Dim ds As New List(Of MonHocDTO)
            Dim i As Integer = 1

            ds = mhBUS.TraCuuMonHoc(mhDTO)
            dgMonHoc.Rows.Clear()

            For Each mh As MonHocDTO In ds
                dgMonHoc.Rows.Add(i, mh.MaMonHoc, mh.TenMonHoc)
                i += 1
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgMonHoc_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgMonHoc.SelectionChanged
        Try
            If (dgMonHoc.SelectedRows.Count > 0) Then
                If (dgMonHoc.SelectedRows(0).Cells(1).Value = Nothing) Then
                    Return
                End If

                txtMaMH.Text = dgMonHoc.SelectedRows(0).Cells(1).Value.ToString().Trim()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub frmChiTietThoiKhoaBieu_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Try
            Dim cttkbBUS As New ChiTietThoiKhoaBieuBUS
            Dim kq As Integer = cttkbBUS.LayBaoCaoThoiKhoaBieu()
            If kq < 0 Then
                MessageBox.Show("Luu Bao Cao that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class